package gui;

import javax.swing.JPanel;

import Entities.RequestEntity;
import common.ChatIF;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;
import java.awt.Color;
import javax.swing.UIManager;
/**
 * 
 * @author Idan Mann
 * This GUI Class presents the user with the ability to track it's requests
 * A list of past requests will be loaded and present the user with it's current status and expected deadline
 *
 *
 */
public class TrackChangePanel extends JPanel implements ChatIF{
	private JComboBox requestsComboBox;
	private JButton btnConfirm;
	private JButton btnBack;
	private AcademicFrame frame;
	private InitiatorPanel initiator;
	private String uid;
	private JTextField statustextField;
	private JTextField deadlinetextField;
	private JTextArea descriptionTextArea;
	public TrackChangePanel(AcademicFrame frame, InitiatorPanel initiator, String pid)
	{
		super();
		this.frame = frame;
		this.initiator = initiator;
		this.uid = pid;
		frame.setSize(500, 400);
		initialize();
	}
	
	void initialize()
	{
		setLayout(null);
		
		JLabel lblTrackChange = new JLabel("Track Change");
		lblTrackChange.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblTrackChange.setBounds(234, 24, 127, 46);
		add(lblTrackChange);
		
		JButton btnCheckStatus = new JButton("Check Status");
		btnCheckStatus.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e)
			{
				if (requestsComboBox.getSelectedItem().toString().equalsIgnoreCase("--Choose a Request--"))
				{
					(new Dialog("Please select a request to track.")).setVisible(true);
				}
				else
				{
					ArrayList<Object> trackRequest = new ArrayList<Object>();
					trackRequest.clear();
					trackRequest.add("trackRequest");
					trackRequest.add(requestsComboBox.getSelectedItem().toString());
					AcademicFrame.myclient.sendMessageToServer(trackRequest);

				}
			}
		});
		btnCheckStatus.setBounds(422, 96, 127, 23);
		add(btnCheckStatus);
		
		JLabel lblRequestDescription = new JLabel("Request Description");
		lblRequestDescription.setBounds(10, 147, 127, 28);
		add(lblRequestDescription);
		
		JLabel lblStatus = new JLabel("Status");
		lblStatus.setBounds(359, 325, 46, 14);
		add(lblStatus);
		
		statustextField = new JTextField();
		statustextField.setEditable(false);
		statustextField.setBounds(410, 322, 139, 20);
		add(statustextField);
		statustextField.setColumns(10);
		
		JLabel lblDeadlineForThe = new JLabel("Next update in");
		lblDeadlineForThe.setBounds(314, 366, 91, 14);
		add(lblDeadlineForThe);
		
		deadlinetextField = new JTextField();
		deadlinetextField.setEditable(false);
		deadlinetextField.setColumns(10);
		deadlinetextField.setBounds(410, 363, 139, 20);
		add(deadlinetextField);
		
		JLabel lblNewLabel = new JLabel("Request History");
		lblNewLabel.setBounds(118, 100, 101, 14);
		add(lblNewLabel);
		
		requestsComboBox = new JComboBox();
		requestsComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		requestsComboBox.setBounds(235, 96, 170, 22);
		add(requestsComboBox);
		
		btnConfirm = new JButton("Confirm");
		btnConfirm.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e)
			{
				ArrayList<Object> command = new ArrayList<Object>();
				command.add("confirmRequest");
				command.add(requestsComboBox.getSelectedItem().toString());
				AcademicFrame.myclient.sendMessageToServer(command);
			}
		});
		btnConfirm.setEnabled(false);
		btnConfirm.setBounds(299, 417, 91, 23);
		add(btnConfirm);
		
		btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				frame.setSize(500, 500);
				frame.setContentPane(initiator);	
			}
		});
		btnBack.setBounds(198, 417, 91, 23);
		add(btnBack);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(131, 148, 440, 156);
		add(scrollPane);
		
		descriptionTextArea = new JTextArea();
		scrollPane.setViewportView(descriptionTextArea);
		descriptionTextArea.setBackground(UIManager.getColor("TextPane.disabledBackground"));
		descriptionTextArea.setEditable(false);
	}

	
	
	
	
	@Override
	public void display(Object message)
	{
		
		ArrayList<Object> answer = new ArrayList<Object>();
		answer = (ArrayList<Object>)message;
		
		if (	((String)(answer.get(0))).equalsIgnoreCase("listOfRequestHistory")	)
		{
			if (	((String)(answer.get(1))).equalsIgnoreCase("0")	)
			{
				(new Dialog ("You have no requests to track. Closing window.")).setVisible(true);
				frame.setSize(500, 500);
				frame.setContentPane(initiator);	
			}
			else
			{
				ArrayList<String> requestHistoryList = (ArrayList<String>)message; 
								
				int numberOfRequests = Integer.parseInt( (requestHistoryList.get(1)).toString() ); // get the number of requests in the list
				this.requestsComboBox.addItem("--Choose a Request--");
				for (int i=0; i < numberOfRequests ; i++ )
				{
					this.requestsComboBox.addItem((String)requestHistoryList.get(i+2));  // add requests to the list
				}
			}


		}
		else
			if (	((String)(answer.get(0))).equalsIgnoreCase("requestData")	)
			{
				
					RequestEntity requestDetails = (RequestEntity)(answer.get(1));
					
					if ( (requestDetails.getStatus()).equalsIgnoreCase("Awaiting confirmation") )
						btnConfirm.setEnabled(true);
				
					descriptionTextArea.setText("You sent the following request on " + requestDetails.getD() + ":\n" + requestDetails.getOCD());
					statustextField.setText(requestDetails.getStatus());
					deadlinetextField.setText(requestDetails.getDeadline());
					
				

			}
			else   
			if (	((String)(answer.get(0))).equalsIgnoreCase("changeStatus")	)
			{	
				(new Dialog("Thank you. Request handling is complete.")).setVisible(true);
				frame.setSize(500, 500);
				frame.setContentPane(initiator);
			}	
		
	}
}
